package com.hdy.orderforthree.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hdy.net.order.bean.DistrictBean;
import com.hdy.net.order.bean.LoginBean;
import com.hdy.net.order.bean.TablesBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;
import com.hdy.orderforthree.adapter.BaseAdapter;
import com.hdy.orderforthree.adapter.DistrictAdapter;
import com.hdy.orderforthree.adapter.TablesAdapter;
import com.hdy.orderforthree.pos.ConcretePosFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TablesPop extends CenterDialogPop {

    @BindView(R2.id.tables_title_recyclerView)
    RecyclerView mDistrictRecyclerView;
    @BindView(R2.id.tables_center_recyclerView)
    RecyclerView mTablesInfoRecyclerView;

    private DistrictAdapter mDistrictAdapter;
    private TablesAdapter mTablesAdapter;
    private List<DistrictBean> mTables;

    public TablesPop(Context context) {
        super(R.layout.tables_layout, context);

        LinearLayoutManager layoutManager = new LinearLayoutManager(mLayout.getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDistrictRecyclerView.setLayoutManager(layoutManager);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mLayout.getContext(), 3);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        mTablesInfoRecyclerView.setLayoutManager(gridLayoutManager);
    }

    public void setAdapter(List<DistrictBean> list) {
        mTables = new ArrayList<>();
        if (list != null && list.size() > 0) {
            mTables.addAll(list);
            mDistrictAdapter = new DistrictAdapter(mTables);
            mDistrictAdapter.setSelectedPosition(0);
            mDistrictAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position) {
                    mTablesAdapter.SetList(mTables.get(position).getTables());
                    mTablesAdapter.setSelectedPosition(-1);
                    mTablesAdapter.notifyDataSetChanged();
                    mTablesInfoRecyclerView.smoothScrollToPosition(0);
                }

                @Override
                public void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }

                @Override
                public void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }

                @Override
                public void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }
            });

            mDistrictRecyclerView.setAdapter(mDistrictAdapter);

            mTablesAdapter = new TablesAdapter(mTables.get(mDistrictAdapter.getSelectedPosition()).getTables());
            mTablesAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position) {
                    DistrictBean district = mTables.get(mDistrictAdapter.getSelectedPosition());
                    TablesBean table = district.getTables().get(position);
                    LoginBean login = ConcretePosFactory.getInstance().getPos().getLoginBean();
                    login.setDistrictId(district.getCode());
                    login.setTables(table);
                    ConcretePosFactory.getInstance().getPos().saveLoginBean(login);

                    if (mUpdateViewCallback != null) {
                        mUpdateViewCallback.showTableInfo();
                    }
                }

                @Override
                public void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }

                @Override
                public void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }

                @Override
                public void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, Object o) {

                }
            });
            mTablesInfoRecyclerView.setAdapter(mTablesAdapter);
        }
    }
}
